package com.wondertek.poms.service.controller;

import com.wondertek.poms.common.constant.Constants;
import com.wondertek.poms.common.exception.MessageCodes;
import com.wondertek.poms.common.utils.validate.ValidateFilter;
import com.wondertek.poms.common.vo.PageBean;
import com.wondertek.poms.common.vo.ResultBean;
import com.wondertek.poms.core.service.IProductService;
import com.wondertek.poms.dao.po.Product;
import com.wondertek.poms.dao.pojo.ProductPojo;
import com.wondertek.poms.service.vo.ProductVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Optional;

@Api(value = "ProductController", description = "产品计费点管理")
@RestController
@RequestMapping(value = "/product")
public class ProductController {

	@Autowired
	private IProductService productService;

	@ApiOperation("产品计费点 列表")
	@GetMapping(value = "/list")
	@ResponseBody
	public ResultBean list(ProductVo productVo,
	                       @RequestParam(defaultValue = "1") int page,
	                       @RequestParam(defaultValue = "20") int rows,
	                       @RequestParam(defaultValue = "UPDATE_TIME") String sidx,
	                       @RequestParam(defaultValue = "desc") String sord) {
		ProductPojo productpojo = new ProductPojo();
		BeanUtils.copyProperties(productVo, productpojo);
		Page<ProductPojo> list = productService.findAll(productpojo, page, rows, sord, sidx);
		return PageBean.ok(list.getTotalPages(), list.getTotalElements(), list.getContent());
	}

	@ApiOperation("产品计费点 添加")
	@PostMapping(value = "/add")
	@ResponseBody
	public ResultBean list(@RequestBody ProductVo productVo) {
		List<String> messageList = ValidateFilter.getFilterMessage(productVo, Constants.VALIDATE_DEFAULT);
		if (!messageList.isEmpty()) {
			return ResultBean.error(messageList.toString());
		}
		if (isRepeat(productVo)) {
			return ResultBean.error(MessageCodes.get(MessageCodes.M980014));
		}

		Product product = new Product();
		BeanUtils.copyProperties(productVo, product);
		Product.putTimeAndUser(product);
		productService.save(product);
		return ResultBean.ok();
	}


	public boolean isRepeat(ProductVo productVo) {
		Product temp = new Product();
		temp.setProductID(productVo.getProductID());
		Optional<Product> productOpt = productService.findOne(Example.of(temp));
		if (productOpt.isPresent()) {
			Product product = productOpt.get();
			if (productVo.getId() != null && productVo.getId() != product.getId())
				return true;
			else if (productVo.getId() == null)
				return true;
		}
		return false;
	}

	@GetMapping("/charge-point")
	public ResultBean getChargePoint() {
		List<Long> allProductId = productService.getAllProductId(null);
		return ResultBean.ok(allProductId);
	}

}
